﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Windows.Forms.Integration;

namespace comm.editor
{
    public class ItemEquipTypeListEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {
                var elHost = new ElementHost
                {
                    Width = 240,
                    Height = 360,
                    Child = new UCItemEquipTypeList((List<objst.ItemEquipType>)value)
                };
                edSvc.DropDownControl(elHost);
            }
            return value;
        }
    }

    /// <summary>
    /// UCItemEquipTypeList.xaml 的交互逻辑
    /// </summary>
    public partial class UCItemEquipTypeList : UserControl
    {
        List<objst.ItemEquipType> _typeList = null;

        public UCItemEquipTypeList(List<objst.ItemEquipType> typeList)
        {
            InitializeComponent();
            _typeList = typeList;

            var names = comm.EnumHelper.GetNames(typeof(comm.objst.ItemEquipType));
            foreach (var name in names)
            {
                cbType.Items.Add(new TextBlock
                {
                    Tag = name,
                    Text = name
                });
            }
            if (cbType.Items.Count > 0)
            {
                cbType.SelectedIndex = 0;
            }

            Fill();

            btnAdd.Click += new RoutedEventHandler(btnAdd_Click);
        }

        void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (cbType.SelectedIndex != -1)
            {
                var tb = (TextBlock)cbType.SelectedItem;
                var type = comm.EnumHelper.Parse<objst.ItemEquipType>(tb.Tag.ToString());
                _typeList.Add(type);
                Fill();
            }
        }

        void Fill()
        {
            spList.Children.Clear();

            tbCount.Text = _typeList.Count.ToString();

            #region sort enum
            _typeList.Sort(new Comparison<objst.ItemEquipType>((t1, t2) =>
            {
                int n1 = (int)t1, n2 = (int)t2;
                if (n1 > n2)
                {
                    return 1;
                }
                else if (n1 < n2)
                {
                    return -1;
                }
                return 0;
            }));
            #endregion

            var names = comm.EnumHelper.GetNames(typeof(comm.objst.ItemEquipType));
            foreach (var type in _typeList)
            {
                var type2 = type;
                var grid = new Grid { Margin = new Thickness(0, 0, 0, 4) };

                #region cb
                var cb = new ComboBox
                {
                    Focusable = false,
                    VerticalAlignment = VerticalAlignment.Center,
                    Margin = new Thickness(0, 0, 24, 0)
                };
                foreach (var name in names)
                {
                    var tb = new TextBlock { Tag = name, Text = name };
                    cb.Items.Add(tb);
                    if (name == type.ToString())
                    {
                        tb.FontWeight = FontWeights.Bold;
                        cb.SelectedItem = tb;
                    }
                }
                cb.SelectionChanged += new SelectionChangedEventHandler((o, e) =>
                {
                    if (cb.SelectedIndex != -1)
                    {
                        var tb = (TextBlock)cb.SelectedItem;
                        var type3 = comm.EnumHelper.Parse<objst.ItemEquipType>(tb.Tag.ToString());
                        _typeList.Remove(type2);
                        _typeList.Add(type3);
                        Fill();
                    }
                });
                grid.Children.Add(cb);
                #endregion

                #region btnDel
                var btnDel = new Button
                {
                    Content = "X",
                    Width = 22,
                    Height = 22,
                    VerticalAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Right
                };
                btnDel.Click += new RoutedEventHandler((o, e) =>
                {
                    _typeList.Remove(type2);
                    Fill();
                });
                grid.Children.Add(btnDel);
                #endregion

                spList.Children.Add(grid);
            }
        }
    }
}
